package com.hyc.seckillsystem.controller;

import com.hyc.seckillsystem.service.redisService;
import com.hyc.seckillsystem.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.PostConstruct;

/**
 * @projectName: seckillsystem
 * @package: com.hyc.seckillsystem.controller
 * @className: redisController
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/3/7 21:32
 * @version: 1.0
 */
@Controller
@Slf4j
public class redisController {
    @Autowired
    redisService redisService;
    @Autowired
    RedisUtil redisUtil;

    @PostConstruct
    public void initdata() {

        redisService.initdata();
    }

    @PostMapping(value = "/seckillAPI")
    public String seckill(String userId, String goodsId, Model model) {
        String key = goodsId + "_" + userId;
        if (userId == null || goodsId == null) {
            return "参数异常";
        }
        String result = redisService.seckill(userId, goodsId);
        if (redisUtil.hasKey(key)) {
            log.info(result);
            model.addAttribute("userId", result);
            model.addAttribute("goodsId", goodsId);
        }
        return "order";
    }
}
